package com.adz.messagingdemo.eventbus;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.adz.messagingdemo.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Created by Administrator on 2017/3/3.
 * 接收消息
 */

public class ReceiverActivity extends Activity {
    Button btn;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiver);
        EventBus.getDefault().register(this);//接收方注册
        btn= (Button) findViewById(R.id.btn_receiver);
        tv= (TextView) findViewById(R.id.display_msg);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ReceiverActivity.this,SendActivity.class));
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//接触注册
    }
    // 在onEventMainThread（）方法上加注解“@Subscribe”，3.0会报
    //EventBusException: Subscriber class *** and its super classes have no public methods with the @Subscribe annotation。
    @Subscribe
    public void onEventMainThread(MessageBean event) {
        String msg =event.getMsg();
        tv.setText(msg);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }


}
